﻿@using EmployeeLeaveSystem.Client.Components
@using EmployeeLeaveSystem.Shared;
@using EmployeeLeaveSystem.Shared.Models;
@inject IMessageService _MessageService;
@inject HttpClient _HttpClient;
@inject ModalService _modalService;

<SalaryRules Id="GobalManager.myInfo?.Department?.Id" />
<Divider />
<h5>工资核算</h5>

<div style="width:100%;display:flex;justify-content: center;margin:20px">
    @{
        var Title = "距离 " + DateTime.Now.Month.ToString() + " 月结束剩余的天数";
    }
    <div><CountDown Title="@Title" Value="@(new DateTime(DateTime.Now.Year,DateTime.Now.Month,DateTime.DaysInMonth(DateTime.Now.Year,DateTime.Now.Month)))" Format="dd 天  h 小时 m 分钟 s 秒" /></div>
</div>

<p>工资发放规则为：基础工资+各种补贴-各种抵扣</p>
<Text Strong>基础工资</Text>
<p><span style="color:#3f8600">@nowRules.BaseSalary　元</span></p>
<div>
    <Text Strong>您的加班信息</Text>
    <p>加班拥有加班补贴，为每小时 @nowRules.AddSalaryOverTime 元</p>
    <Table DataSource="listAddWOM">
        <PropertyColumn Property="x=>x.Id"></PropertyColumn>
        <PropertyColumn Property="x=>x.Created"></PropertyColumn>
        <PropertyColumn Property="x=>x.StartTime"></PropertyColumn>
        <PropertyColumn Property="x=>x.EndTime"></PropertyColumn>
        <PropertyColumn Property="x=>(x.EndTime-x.StartTime).TotalHours"></PropertyColumn>
        <PropertyColumn Property="x=>x.AcceptStaff.Name"></PropertyColumn>
        <ActionColumn>
            <Button OnClick="()=>{OpenModalWOM(context.Id);}">查看</Button>
        </ActionColumn>
    </Table>
    <div style="margin:10px">
        <div style="display:flex;justify-content: center;
    align-items: center;">
            <Icon Type="trophy" Theme="outline" />
            <div>您这个月加班时间是：@totalAddWorkHours 小时，所获的补贴是　<span style="color:#3f8600">@totalAddWorkHours * @nowRules.AddSalaryOverTime = @(totalAddWorkHours * nowRules.AddSalaryOverTime)　元</span> </div>
        </div>
    </div>
</div>
<div>
    <Text Strong>您的出勤信息</Text>
    <p>迟到将会被扣款，每一次迟到扣款为 @nowRules.CutSalaryComeLate 元</p>
    <Table DataSource="listAddWork">
        <PropertyColumn Property="x=>x.Id"></PropertyColumn>
        <PropertyColumn Property="x=>x.Created"></PropertyColumn>
        <PropertyColumn Property="x=>x.StartTime"></PropertyColumn>
        <PropertyColumn Property="x=>x.EndTime"></PropertyColumn>
        <PropertyColumn Property="x=>x.workState"></PropertyColumn>
        <PropertyColumn Property="x=>x.AcceptStaff.Name"></PropertyColumn>
        <ActionColumn>
            <Button OnClick="()=>{OpenModalAddWork(context.Id);}">查看</Button>
        </ActionColumn>
    </Table>
    <div style="margin:10px">
        <div style="display:flex;justify-content: center;
    align-items: center;">
            <Icon Type="trophy" Theme="outline" />
            <div>您本月工作时间是：<span style="color:#3f8600">@totalWorkTime 小时</span></div>
        </div>
        <div style="display:flex;justify-content: center;
    align-items: center;">
            <Icon Type="frown" Theme="outline" />
            <div>您本月迟到次数是：@lateCome 次，罚款为　<span style="color:#cf1322">@lateCome * @nowRules.CutSalaryComeLate = @(lateCome * nowRules.CutSalaryComeLate)　元</span></div>
        </div>
    </div>
</div>
<div>
    <Text Strong>本月预计薪水</Text>
    <p>根据上方核算的结果以及制定的规则，您所获得的薪水计算如下：</p>
    <GridRow Gutter="3" Justify="space-around" Align="middle">
        <GridCol>
            <Statistic Title="基本薪水（CNY）" Value="@nowRules.BaseSalary" Precision="2" />
        </GridCol>
        <div style="font-size: 30px;font-weight: 100;">
            +
        </div>
        <GridCol>
            <Statistic Title="奖励薪水（CNY）" Value="@(totalAddWorkHours * nowRules.AddSalaryOverTime)" Precision="2" />
        </GridCol>
        <div style="font-size: 30px;font-weight: 100;">-</div>
        <GridCol>
            <Statistic Title="扣除薪水（CNY）" Value="@(lateCome * nowRules.CutSalaryComeLate)" Precision="2" />
        </GridCol>
    </GridRow>
</div>
<Paragraph>
    <PieSalary AddSalary="@(totalAddWorkHours * nowRules.AddSalaryOverTime)" BaseSalary="@nowRules.BaseSalary" CutSalary="@(lateCome * nowRules.CutSalaryComeLate)" />
</Paragraph>
<Paragraph>
    <GridRow Justify="left">
        <GridCol>
            <Statistic ValueStyle="color:green;" Title="最终薪水（CNY）" Value="@(nowRules.BaseSalary+(totalAddWorkHours * nowRules.AddSalaryOverTime)-(lateCome * nowRules.CutSalaryComeLate))" Precision="2" />
        </GridCol>
    </GridRow>
</Paragraph>
<Paragraph />
<div>
    <Button Block Type="primary" OnClick="AddSalaryInfo">申请结算工资</Button>
</div>
<Paragraph />

@code {
    NowRules nowRules = new();
    List<AddWOM> listAddWOM = new();
    List<AddWork> listAddWork = new();
    double totalAddWorkHours = 0, lateCome = 0, totalWorkTime = 0; // 总计加班次数，总计迟到次数，总共工作时间
    protected override async Task OnInitializedAsync() {
        try {
            nowRules = await _HttpClient.GetFromJsonAsync<NowRules>($"/api/Salary/GetRulesByDepartmentId?id={GobalManager.myInfo?.Department?.Id}");
            listAddWOM = await _HttpClient.GetFromJsonAsync<List<AddWOM>>($"api/Salary/GetAddWOMInfoByUsrIdAndMonth?usrId={GobalManager.myInfo?.Id}&month={DateTime.Now.Month}");
            listAddWork = await _HttpClient.GetFromJsonAsync<List<AddWork>>($"api/Salary/GetAddWorkInfoByUsrIdAndMonth?usrId={GobalManager.myInfo?.Id}&month={DateTime.Now.Month}");
            await InvokeAsync(() => {
                foreach (var item in listAddWOM) {
                    totalAddWorkHours += (item.EndTime - item.StartTime).TotalHours;
                }
                foreach (var item in listAddWork) {
                    totalWorkTime += (item.EndTime - item.StartTime).TotalHours;
                    if (item.workState == WorkState.迟到) lateCome += 1;
                }
            });
        }
        catch (Exception ex) {
        }
        InvokeAsync(StateHasChanged);
    }

    private async Task OpenModalAddWork(int Id) {
        var modalConfig = new ModalOptions {
                    Title = "详情",
                    Draggable = true,
                    Footer = null,
                    Maximizable = true,
                    Width = "80vw",
                    Content = @<EmployeeLeaveSystem.Client.Pages.WorkManager.ViewWorkManager Id="Id" />
            };
        await _modalService.CreateModalAsync(modalConfig);
    }

    private async Task OpenModalWOM(int Id) {
        var modalConfig = new ModalOptions {
                    Title = "详情",
                    Draggable = true,
                    Footer = null,
                    Maximizable = true,
                    Width = "80vw",
                    Content = @<EmployeeLeaveSystem.Client.Pages.WorkOvertimeManager.ViewWOMManager Id="Id" />
        };
        await _modalService.CreateModalAsync(modalConfig);
    }

    private async Task AddSalaryInfo() {
        SalaryInfo salaryInfo = new();
        salaryInfo.StaffId = GobalManager.myInfo.Id;
        salaryInfo.Year = DateTime.Now.Year;
        salaryInfo.Month = DateTime.Now.Month;
        salaryInfo.BaseSalary = nowRules.BaseSalary;
        salaryInfo.AddLiveSalary = totalAddWorkHours * nowRules.AddSalaryOverTime;
        salaryInfo.CutOffSalary = lateCome * nowRules.CutSalaryComeLate;
        await _HttpClient.PostAsJsonAsync("api/Salary/AddSalaryInfo", salaryInfo);
        await _MessageService.Success("成功上传");
    }
}
